package com.cskaoyan.controller;

import com.cskaoyan.aspect.OperationLog;
import com.cskaoyan.bean.vo.BaseRespVo;
import com.cskaoyan.bean.DataBean;
import com.cskaoyan.bean.*;
import com.cskaoyan.realm.MallToken;
import com.cskaoyan.service.AuthenticateService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;



/**
 * 登录
 */

@RestController
@RequestMapping("admin/auth")
public class AuthenticateController {

    @Autowired
    AuthenticateService authenticateService;

    @OperationLog
    @RequestMapping("/login")
    public BaseRespVo login(@RequestBody AdminLoginVO user) {
        Subject subject = SecurityUtils.getSubject();
        try {
            //在自定义的realm中处理
            subject.login(new MallToken(user.getUsername(),user.getPassword(),"admin"));
        } catch (AuthenticationException e) {
            return BaseRespVo.fail(605,"用户帐号或密码不正确");
        }
        //拿到sessionId
        String id = (String) subject.getSession().getId();
        return BaseRespVo.ok(id);
    }

    @OperationLog
    @RequestMapping("logout")
    public BaseRespVo logout() {
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return BaseRespVo.ok();
    }

    @RequestMapping("/info")
    public BaseRespVo info(String token) {
        DataBean dataBean = authenticateService.info();
        if (dataBean.getName() == null) {
            return BaseRespVo.ok();
        }
        return BaseRespVo.ok(dataBean);
    }
  /*@RequestMapping("/login")
  public BaseRespVo login(@RequestBody Map user){
      //登录的业务逻辑使用shiro来做 →
      return BaseRespVo.ok("4e295462-c349-461a-8e79-e8147ca1ff1a");
  }

    @RequestMapping("/info")
    public BaseRespVo info(String token){
        DataBean dataBean = new DataBean();
        dataBean.setAvatar("http://localhost:8081/banner.png");
        dataBean.setName("ligenli");
        ArrayList<String> perms = new ArrayList<>();
        perms.add("*");
        dataBean.setPerms(perms);

        ArrayList<String> roles = new ArrayList<>();
        roles.add("超级管理员");
        dataBean.setRoles(roles);
        return BaseRespVo.ok(dataBean);
    }*/

}
